
package com.mc.lp.database.service.handler.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.mc.lp.core.util.CollectionUtil;
import com.mc.lp.shared.vo.AcademicRecordVO;

/**
 * 
 * @author deepak
 *
 */
public class GradeAggregator {
	List<AcademicRecordVO> academics;
	List<AggregatedGrades> aggregatedGrades;
	HashMap<Integer, ArrayList<AggregatedGrades>> gradeMap= new HashMap<Integer, ArrayList<AggregatedGrades>>();
	public GradeAggregator(List<AcademicRecordVO> academics,List<AggregatedGrades> aggregatedGrades){
		this.academics=academics;
		this.aggregatedGrades=aggregatedGrades;
	}
	
	public void aggregate(){
		if(CollectionUtils.isEmpty(academics) || aggregatedGrades==null){
			return;
		}
		HashSet<AggregatedGrades> gradeSet= new HashSet<AggregatedGrades>();
		GradeWiseCount countMap = new GradeWiseCount();
		for(AcademicRecordVO academic: this.academics ){
				AggregatedGrades tempGrade =  new AggregatedGrades();
				tempGrade.setGradeId(academic.getGradeId());
				tempGrade.setSubjectId(academic.getSubjectId());
				if(gradeSet.contains(tempGrade)){
					AggregatedGrades existingGrade = CollectionUtil.getObject(gradeSet, tempGrade);
					int existingWeightage=existingGrade.getWeightage();
					existingGrade.setWeightage(existingWeightage+academic.getAttributeWeightage());
					gradeSet.add(existingGrade);
				}else{
					tempGrade.setWeightage(academic.getAttributeWeightage());
					tempGrade.setPosition(countMap.getCount(academic.getGradeId()));
					gradeSet.add(tempGrade);
				}
		}
		//add all to given list;
		aggregatedGrades.addAll(gradeSet);
		
		//now group by grade id .
		for(AggregatedGrades grades: aggregatedGrades){
			ArrayList<AggregatedGrades> aggregatedGradeList=gradeMap.get(grades.getGradeId());
			if(aggregatedGradeList ==null){
				aggregatedGradeList = new ArrayList<AggregatedGrades>();
			}
			aggregatedGradeList.add(grades);
			gradeMap.put(grades.getGradeId(), aggregatedGradeList);
		}
	}
	
	public HashMap<Integer, ArrayList<AggregatedGrades>> getGradeMap() {
		return gradeMap;
	}

	public void setGradeMap(HashMap<Integer, ArrayList<AggregatedGrades>> gradeMap) {
		this.gradeMap = gradeMap;
	}


	public class GradeWiseCount{
		public HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		public Integer getCount(Integer gradeId){
			Integer count=map.get(gradeId);
			if(count==null){
				count=1;
				map.put(gradeId, count);
			}
			else{
				count+=4;
				map.put(gradeId, count);
			}
			return count;
		}
	}

	public List<AcademicRecordVO> getAcademics() {
		return academics;
	}

	public void setAcademics(List<AcademicRecordVO> academics) {
		this.academics = academics;
	}

	public List<AggregatedGrades> getAggregatedGrades() {
		return aggregatedGrades;
	}

	public void setAggregatedGrades(List<AggregatedGrades> aggregatedGrades) {
		this.aggregatedGrades = aggregatedGrades;
	}
	
	
	
}
